#! /usr/bin/env python

"""
(c) 2007 Alon Zakai ('Kripken') <kripkensteiner@gmail.com>

This software is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this library; see the file COPYING
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Contains GPL code fragments by David Boddie <david@boddie.org.uk>
"""

### NEED package "fuse-utils", for fusermount
### NEED user to be a member of the group "fuse" (what is command for dis?)
### NEED to execute "chgrp fuse /dev/fuse" on Edgy (bug 1860)

import os, sys
import time


from fuse import Fuse

class ODMFS_Error(Exception):
	pass

class ODMFS(Fuse):

	def __init__(self, *args, **kwargs):
		print "__init__", args, kwargs

		path = kwargs.get("path", None)
#		if path is None:
#			raise ODMFS_Error, "No path selected"

		Fuse.__init__(self, *args, **kwargs)

	def getattr(self, path):
		print "getattr", path
		return (16877, 407201L, 2050L, 1, 1000, 1000, 4096L, 1170781446, 1170781445, 1170781445)

		temp = os.lstat("/media/sda2/svn/alonz")
		print temp
		return temp
#		return (16877, 407201L, 2050L, 1, 1000, 1000, 0L, time.time(), time.time(), time.time())
		# Last time should be the creation time
#		temp = os.lstat("/media/sda2/svn/alonz")
#		return (33188, 407253L, 2050L, 1, 1000, 1000, 8375L, 1169973741, 1170419728, 1170419728)
#		return (33188, 422511L, 769L,  1, 1032, 100,  926L,  1105022698, 1105022732, 1105022732)

#st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):	

	def readlink(self, path):
		print "readlink", path
		return "/home/alon/errorlog.txt"

	def getdir(self, path):
		print "getdir", path
		files = ['zorro', 'morbo', 'homer']
		return map(lambda x: (x,0), files)

	def unlink(self, path):
		print "unlink", path
		return None

	def rmdir(self, path):
		print "rmdir", path
		return None

	def symlink(self, src, dest):
		print "symlink", path
		return None

	def rename(self, src, dest):
		print "rename", path
		return None

	def link(self, src, dest):
		print "link", path
		return None

	def chmod(self, path, mode):
		print "chmod", path
		return None

	def chown(self, path, user, group):
		print "chown", path
		return None

	def truncate(self, path, size):
		print "truncate", path
		return None

	def mknod(self, path, mode, dev):
		print "mknod", path
		return None

	def mkdir(self, path, mode):
		print "mkdir", path
		return None

	def utime(self, path, times):
		print "utime", path
		return None

	def open(self, path, flags):
		print "open", path
		return None

	def read(self, path, length, offset):
		print "read", path
		return None

	def write(self, path, buf, offset):
		print "write", path
		return None

	def release(self, path, flags):
		print "release", path
		return None
#		return 0

	def statfs(self):		
		print "statfs"
		return (0,0,0,0,0)

	def fsync(self, path, isfsyncfile):
		print "fsync", path
		return None
#		return 0

if __name__ == "__main__":

	if len(sys.argv) < 3:
	
		sys.stderr.write(
			"Usage: %s [ODMFS options] <mount point> <image>\n" % sys.argv[0]+\
			"	   %s -u <mount point>\n" % sys.argv[0]
			)
		sys.exit(1)

	if sys.argv[-2] == "-u":
		mount = 0
		mount_point = os.path.abspath(sys.argv[-1])
	else:
		mount = 1
		mount_point = os.path.abspath(sys.argv[-2])
	
	if mount == 1:
	
		created_mount_point = 0
		
		if not os.path.exists(mount_point):
		
			os.mkdir(mount_point)
			created_mount_point = 1
		
		elif not os.path.isdir(mount_point):
		
			sys.stderr.write("Cannot use %s as a mount point\n" % mount_point)
			sys.exit(1)

		#sys.argv[:-1]
		server = ODMFS(sys.argv[:-1], path = sys.argv[-1])
#		server.multithreaded = 1
		server.main()
		
		if created_mount_point == 1:
		
			os.rmdir(mount_point)
	else:
	
		os.system("fusermount -u %s" % mount_point)
	
	sys.exit(0)
